{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Populating the interactive namespace from numpy and matplotlib\n"
     ]
    }
   ],
   "source": [
    "%pylab inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.append('/Your/Location/To/CEECNetRepo/')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/local/lib/python3.7/site-packages/joblib/_multiprocessing_helpers.py:45: UserWarning: [Errno 28] No space left on device.  joblib will operate in serial mode\n",
      "  warnings.warn('%s.  joblib will operate in serial mode' % (e,))\n"
     ]
    }
   ],
   "source": [
    "from mxnet import nd \n",
    "from ceecnet.models.changedetection.mantis.mantis_dn import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "depth:= 0, nfilters: 32, nheads::4, widths::1\n",
      "depth:= 1, nfilters: 64, nheads::8, widths::1\n",
      "depth:= 2, nfilters: 128, nheads::16, widths::1\n",
      "depth:= 3, nfilters: 256, nheads::32, widths::1\n",
      "depth:= 4, nfilters: 512, nheads::64, widths::1\n",
      "depth:= 5, nfilters: 1024, nheads::128, widths::1\n",
      "depth:= 6, nfilters: 512, nheads::128, widths::1\n",
      "depth:= 7, nfilters: 256, nheads::64, widths::1\n",
      "depth:= 8, nfilters: 128, nheads::32, widths::1\n",
      "depth:= 9, nfilters: 64, nheads::16, widths::1\n",
      "depth:= 10, nfilters: 32, nheads::8, widths::1\n"
     ]
    }
   ],
   "source": [
    "# D6nf32 example \n",
    "depth=6\n",
    "norm_type='GroupNorm'\n",
    "norm_groups=4\n",
    "ftdepth=5\n",
    "NClasses=2\n",
    "nfilters_init=32\n",
    "psp_depth=4\n",
    "nheads_start=4\n",
    "\n",
    "\n",
    "net = mantis_dn_cmtsk(nfilters_init=nfilters_init, NClasses=NClasses,depth=depth, ftdepth=ftdepth, model='CEECNetV1',psp_depth=psp_depth,norm_type=norm_type,norm_groups=norm_groups,nheads_start=nheads_start)\n",
    "net.initialize()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "BatchSize = 4\n",
    "img_size=256\n",
    "NChannels = 3\n",
    "\n",
    "input_img_1 = nd.random.uniform(shape=[BatchSize, NChannels, img_size, img_size])\n",
    "input_img_2 = nd.random.uniform(shape=[BatchSize, NChannels, img_size, img_size])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example of forward operation:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "outs = net(input_img_1, input_img_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(4, 2, 256, 256)\n",
      "(4, 2, 256, 256)\n",
      "(4, 2, 256, 256)\n"
     ]
    }
   ],
   "source": [
    "# outs is a list of outputs, segmentation, boundary, distance. \n",
    "# Each has shape BatchSize, NClasses, img_size, img_size\n",
    "for out in outs:\n",
    "    print (out.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Example of performing backward with multitasking operation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "labels_segm = nd.random.uniform(shape=[BatchSize, NClasses, img_size, img_size])\n",
    "labels_segm = labels_segm > 0.5\n",
    "\n",
    "labels_bound = nd.random.uniform(shape=[BatchSize, NClasses, img_size, img_size])\n",
    "labels_bound = labels_bound > 0.5\n",
    "\n",
    "labels_dist = nd.random.uniform(shape=[BatchSize, NClasses, img_size, img_size])\n",
    "\n",
    "\n",
    "labels = [labels_segm,labels_bound,labels_dist]\n",
    "labels = nd.concat(*labels,dim=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "from mxnet import autograd\n",
    "from ceecnet.nn.loss.mtsk_loss import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "myMTSKL = mtsk_loss()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "with autograd.record():\n",
    "    listOfPreds = net(input_img_1, input_img_2)\n",
    "    loss = myMTSKL.loss(listOfPreds,labels)\n",
    "    loss.backward()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4,)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "loss.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       "[0.50219935 0.5020496  0.5023406  0.5021815 ]\n",
       "<NDArray 4 @cpu(0)>"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "loss"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
